package com.kkang.ch3.taskexecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @author yongk_liu
 **/
@Configuration
@ComponentScan("com.kkang.ch3.taskexecutor")
@EnableAsync//开启对异步的支持
public class TaskExecutorConfig implements AsyncConfigurer {//实现AsyncConfigurer接口

    @Override
    public Executor getAsyncExecutor() {//获取一个执行器
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//这里使用线程池的多线程执行器
        executor.setCorePoolSize(10);//核心处理线程数量
        executor.setQueueCapacity(150);//排队数量
        executor.setMaxPoolSize(50);//最大
        executor.setKeepAliveSeconds(3);//线程空闲回收超时

        executor.initialize();//创建
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
